package com.net.oil.user.model;

import com.net.oil.bean.BaseBean;
import com.net.oil.base.BaseModel;
import com.net.oil.http.AppHttpClient;
import com.net.oil.http.custom.BaseObserverNoT;
import com.net.oil.user.contract.SetContract;
import com.net.oil.user.presenter.SetPresenter;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 作者：lch
 * 时间：2018/3/31:16:16
 * 邮箱：245850413@qq.com
 * 说明：
 */

public class SetModel extends BaseModel implements SetContract.Model {

    private SetPresenter presenter;

    public SetModel(SetPresenter presenter) {
        this.presenter = presenter;
        compositeDisposable = new CompositeDisposable();
    }

    @Override
    public void logout() {
        AppHttpClient.ApiService().logout()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserverNoT() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        super.onSubscribe(d);
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(@NonNull BaseBean tResultBean) {
                        super.onNext(tResultBean);
                        if (tResultBean != null){
                            presenter.logoutSuccess();
                        }else {
                            presenter.logoutFailed();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        super.onError(e);
                        presenter.logoutFailed();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }
                });
    }
}
